//保存工具类
// const cache = require('./ACache');
import cache from './ACache.js'
import cont from './Constant.js'
import toast from './Toast.js'
/**
 * 普通网络请求
 */
let isShowToken = true
function httpPost(option) {
	let that = this;
	let shortToken = cache.getAsString(cont.CACHE_TOKEN);
	return new Promise(function (resolve, reject) {
		uni.request({
			url:cont.HTTP_URL + option.url,
			data: option.data,
			method: option.method||"POST",
			header: {
				'content-type':'application/json',
				"Authorization":shortToken ?'Bearer ' + shortToken : ''
			},
			success: res => {
				// console.log('--------------------->',res);
				let pack = res.data
				// 成功
				if(res.statusCode == 200){
					resolve(pack);
				}
				// token 过期，提示重新登录
				else if(res.statusCode == 401){
					if(isShowToken){
						isShowToken = false
						uni.showModal({
							title:"温馨提示",
							content:"您的登录认证已过期，请重新登录！",
							confimrText:'前往登录',
							confirmColor:'#EC5B5B',
							showCancel:false,
							success: function (res) {
								if (res.confirm) {
									//清空用户数据
									getApp().globalData.userInfo = null
									//清空全部保存信息
									cache.remove(cont.CACHE_USER)
									cache.remove(cont.CACHE_TOKEN)
									//跳转到登录页
									uni.reLaunch({url:'/pages/login/login'});
									//初始化标记
									isShowToken = true
								}
							}
						});
					}
					resolve(pack);
				}
				//其他原因
				else{
					console.log('-------------->','失败');
					reject(res)
				}
			},
			fail: (err,e) => {
				console.log('-------------->',err,e);
				reject(err)
			},
		});
	})
}

/**
 * 文件上传
 */
function httpUpload(option) {
	let that = this;
	let shortToken = cache.getAsString(cont.CACHE_TOKEN);
	return new Promise(function (resolve, reject) {
		uni.uploadFile({
			url:cont.HTTP_URL + option.url,
			// 本地上传读取到的url
			filePath:option.path,
			name:'fileInput',
			method: option.method||"POST",
			header: {
				'Authorization':shortToken ?'Bearer ' + shortToken : ''
			},
			success: res => {
				let pack = res.data
				// 成功
				if(res.statusCode == 200){
					resolve(pack);
				}
				// token 过期，提示重新登录
				else if(res.statusCode == 401){
					uni.showModal({
						title:"温馨提示",
						content:"您的登录认证已过期，请重新登录！",
						confimrText:'前往登录',
						confirmColor:'#EC5B5B',
						showCancel:false,
						success: function (res) {
							if (res.confirm) {
								//清空用户数据
								getApp().globalData.userInfo = null
								//清空全部保存信息
								cache.remove(cont.CACHE_USER)
								cache.remove(cont.CACHE_TOKEN)
								//跳转到登录页
								uni.reLaunch({url:'/pages/login/login'});
							}
						}
					});
					resolve(pack);
				}
				//其他原因
				else{
					console.log('-------------->','失败');
					reject(res)
				}
			},
			fail: (err,e) => {
				console.log('-------------->',err,e);
				reject(err)
			},
		});
	})
}

//请求数据字典
async function loadDicList(codeList){
	let option={
		url:'api/Sys_Dictionary/GetVueDictionary',
		method:'post',
		data:codeList
	}
	let res=await httpPost(option);
	return res;
}

//TODO 设置
export default{
  post: httpPost,
  upload: httpUpload,
  loadDicList:loadDicList,
}